#!/usr/bin/perl

use strict;
use lib '../trunk';
use WWW::Metaweb;

# Used to view a Perl object response, also the most useful module - ever.
use Data::Dumper;

=head1 NAME

metaweb-example.pl - Shows how to use the WWW::Metaweb module

=head1 SYNOPSIS

perl metaweb-example.pl username password

=head1 DESCRIPTION

This Perl script gives a simple example of how to use the WWW::Metaweb module. I will hopefully be extending it to contain more complex examples when I have the time.

If you've got any questions about stuff here, please feel free to email me:
hds (at) caffeineconcepts.com

=cut

my $mh = WWW::Metaweb->connect(	server => 'sandbox.freebase.com',
				auth_uri => '/api/account/login',
				read_uri => '/api/service/mqlread',
				write_uri => '/api/service/mqlwrite',
				trans_uri => '/api/trans',
				pretty_json => 1,
				debug => 1 );
die $WWW::Metaweb::errstr unless defined $mh;

my ($query_perl, $query_json, $res, $query2, $a_guid);

$a_guid = "#9202a8c04000641f800000000068e429";

# An example query as a perl object. Yes, I'm a Runaways fan.
$query_perl = {
          name => "Nico Minoru",
          id => undef,
          type => [],
          '/comic_books/comic_book_character/cover_appearances' => [{
                name => undef
           }]
};

$query2 = {
	id => '#9202a8c04000641f8000000000300244',
	name => undef,
	type => '/business/business_chain',
	location => [{
		type => '/business/retail_location',
		name => undef
	}]
};

# An example query as a JSON string, all the white space is ignored (as long as
# it's outside quotes obviously).
$query_json = '{"query":{
	"name":"Nico Minoru",
	"id":null,
	"type":[],
	"/comic_books/comic_book_character/cover_appearances":[{
		"name":null
	}]
}
}';

# Ask the user what they want to do.
print '
1) Perl object
2) JSON string
3) A Blurb
4) Enter your own Perl object
5) Enter your own JSON string
6) Enter article ID for a blurb

Choose: ';
my $sel = <STDIN>;

# User enters perl code which gets eval-ed into a query (then run the standard
# perl query code, same as for the example).
if ($sel == 4)  {
	print "Enter perl code (ctrl+d to end):\n";

	my @input = <STDIN>;
	my $code = join '', @input;
	$query_perl = eval $code;
	$sel = 1; 
}
# User enters json string which gets stuck in a string (then run the standard
# json query code, same as for the example).
elsif ($sel == 5)  {
	print "Enter JSON query (ctrl+d to end):\n";

	my @input = <STDIN>;
	$query_json = join '', @input;
	$sel = 2;
}
elsif ($sel == 6)  {
	print "Enter a guid (with preceding '#' or '\%23'):\n";
	chomp($a_guid = <STDIN>);
	
	$sel = 3;
}


if ($sel == 1)  { # Perl obj stuff
	$mh->add_query('read', $query_perl);
	$mh->add_query('read', query2 => $query2);
	$mh->send_envelope('read')
	  or die $WWW::Metaweb::errstr;

	print "Perl structure query:\n";
	print $WWW::Metaweb::errstr . "\n" unless $mh->result_is_ok;
	print Dumper $mh->result;
}
elsif ($sel == 2)  { # JSON string stuff
	$mh->add_query('read', netmetawebquery => $query_json, query2 => $query2);
	$mh->send_envelope('read')
	  or die $WWW::Metaweb::errstr;


	print "JSON query:\n";
	print $WWW::Metaweb::errstr . "\n" unless $mh->result_is_ok;
	print $mh->result . "\n";
}
elsif ($sel == 3)  { # Get a blurb
	# convenience method for $mh->trans('blurb', $a_guid)
	my $text = $mh->blurb($a_guid)
	  or die $WWW::Metaweb::errstr;

	print "A blurb:\n$text\n";
}

if ($sel == 1 || $sel == 2)  {
	if ($mh->result_is_ok('query2'))  {
		print "The second query:\n";
		print $mh->result('query2', 'json') . "\n";
	}
	else  {
		print "The second query was broken.\n";
	}
}


exit;

